package com.hankol.winecellar;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baselibrary.Constants;
import com.baselibrary.activity.BaseActivity;
import com.baselibrary.adapter.CommonIndicatorAdapter;
import com.baselibrary.router.ActivityRouter;
import com.baselibrary.utils.StatusBarUtil;
import com.baselibrary.view.SpacesHorizontalDec;
import com.google.android.material.tabs.TabLayout;
import com.hankol.winecellar.databinding.ActivityImgVideoDetailBinding;
import com.hankol.winecellar.fragment.ImgVideoDetailFragment;
import com.liaoinstan.springview.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/*图片视频评论详情*/
@Route(path = ActivityRouter.WINE_CELLAR.IMG_VIDEO_DETAIL)
public class ImgVideoDetailActivity extends BaseActivity {

    private ActivityImgVideoDetailBinding mBinding;
    private List<String> mUrls = new ArrayList<>();
    private ArrayList<Fragment> mFragments;
    private int mIndex;
    private List<String> commentContentStr = new ArrayList<>();//内容list
    private List<String> titleContentStr = new ArrayList<>();//标题list
    private static final String TAG = "BigImgViewActivity";
    private CommonIndicatorAdapter mWikiIndicatorAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_img_video_detail;
    }

    @Override
    protected void init() {
        setStatusBarDarkTheme(false);
        StatusBarUtil.setTranslucentStatus(this);
        mBinding = getDataBinding();
//        viewModel = getActivityViewModelProvider(this).get(ConfirmOrderViewModel::class.java);
        initView();
        initData();
        loaData();
        initListener();
    }

    @SuppressLint("ResourceType")
    private void initView() {
        mBinding.tlImager.setSelectedTabIndicatorHeight(0);
        mBinding.rvTableIndicator.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        mBinding.rvTableIndicator.addItemDecoration(new SpacesHorizontalDec(DensityUtil.dip2px(this, 5)));
    }

    private void initData() {
        mUrls = getIntent().getStringArrayListExtra(Constants.KEY_URLS);
        mIndex = getIntent().getIntExtra(Constants.KEY_INDEX, 0);
        commentContentStr = getIntent().getStringArrayListExtra(Constants.KEY_COMMENT_CONTENT);
        titleContentStr = getIntent().getStringArrayListExtra(Constants.KEY_COMMENT_TITLE);

        mWikiIndicatorAdapter = new CommonIndicatorAdapter(this, getLayoutInflater());
        mBinding.rvTableIndicator.setAdapter(mWikiIndicatorAdapter);
        mFragments = new ArrayList<>();
        for (int i = 0; i < mUrls.size(); i++) {
            ImgVideoDetailFragment mFragment = new ImgVideoDetailFragment();
            setArguments(i, mFragment);
            mFragments.add(mFragment);
        }
        mBinding.tvTitleContent.setText(mIndex + "/" + mUrls.size());
        mWikiIndicatorAdapter.onRefreshAdapter(mUrls);
//        mBinding.tvDetailsDes.setTextColor(R.color.white, R.color.white);
        mBinding.llBottomBg.setOnClickListener(v -> {
        });
        mBinding.tvTitle.setText(titleContentStr.size() > 0 ? titleContentStr.get(mIndex) : "");
        mBinding.llBottomBg.setVisibility((titleContentStr.size()==0&&commentContentStr.size()==0) ? View.GONE : View.VISIBLE);
        if (commentContentStr.size() > 1) {
            mBinding.tvDetailsDes.setContent(commentContentStr.get(mIndex));
        } else {
            if (commentContentStr.size() == 0) {
                mBinding.tvDetailsDes.setContent("");
            } else {
                mBinding.tvDetailsDes.setContent(commentContentStr.get(0));
            }
            mBinding.llBottomBg.setVisibility((titleContentStr.size()==0&&commentContentStr.size()==0) ? View.GONE : View.VISIBLE);
            mBinding.tvTitle.setText(titleContentStr.size() > 0 ? titleContentStr.get(0) : "");
        }
    }

    private void loaData() {

        //适配器（容器都需要适配器）
        mBinding.vpImager.setOffscreenPageLimit(mFragments.size()-1);//取消预加载
        mBinding.vpImager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            //选中的item
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return "";
            }
        });
        //将TabLayout与Viewager绑定
        mBinding.tlImager.setupWithViewPager(mBinding.vpImager);
        mBinding.vpImager.setCurrentItem(mIndex, true);
    }

    private void initListener() {
        mBinding.ivTitleClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mBinding.tlImager.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Log.d(TAG, "onTabSelected: " + tab.getPosition());
                //选择时触发
                mWikiIndicatorAdapter.onRefreshAdapter(tab.getPosition());
                mBinding.tvTitleContent.setText((tab.getPosition() + 1) + "/" + mUrls.size());
                mBinding.rvTableIndicator.smoothScrollToPosition(tab.getPosition());
//                mBinding.tvDetailsDes.setTextColor(R.color.white, R.color.white);
                mBinding.tvTitle.setText(titleContentStr.size() > 0 ? titleContentStr.get(tab.getPosition()) : "");
                mBinding.llBottomBg.setVisibility((titleContentStr.size()==0&&commentContentStr.size()==0) ? View.GONE : View.VISIBLE);
                if (commentContentStr.size() > 1) {
                    mBinding.tvDetailsDes.setContent(commentContentStr.get(tab.getPosition()));
                } else {
                    if (commentContentStr.size() == 0) {
                        mBinding.tvDetailsDes.setContent("");
                    } else {
                        mBinding.tvDetailsDes.setContent(commentContentStr.get(0));
                    }
                    mBinding.llBottomBg.setVisibility((titleContentStr.size()==0&&commentContentStr.size()==0) ? View.GONE : View.VISIBLE);
                    mBinding.tvTitle.setText(titleContentStr.size() > 0 ? titleContentStr.get(0) : "");
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //未选择是触发
//                setLastViewAlpha(tab.getCustomView());
                mWikiIndicatorAdapter.onRefreshAdapter(tab.getPosition());
                mBinding.rvTableIndicator.smoothScrollToPosition(tab.getPosition());
                Log.d(TAG, "onTabUnselected: " + tab.getPosition());
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mBinding.vpImager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mBinding.tlImager));

        mBinding.vpImager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
                mBinding.tvTitleContent.setText((i + 1) + "/" + mUrls.size());
//                mBinding.tvDetailsDes.setTextColor(R.color.white, R.color.white);
                mBinding.tvTitle.setText(titleContentStr.size() > 0 ? titleContentStr.get(i) : "");
                mBinding.llBottomBg.setVisibility((titleContentStr.size()==0&&commentContentStr.size()==0) ? View.GONE : View.VISIBLE);
                if (commentContentStr.size() > 1) {
                    mBinding.tvDetailsDes.setContent(commentContentStr.get(i));
                } else {
                    if (commentContentStr.size() == 0) {
                        mBinding.tvDetailsDes.setContent("");
                    } else {
                        mBinding.tvDetailsDes.setContent(commentContentStr.get(0));
                    }
                    mBinding.llBottomBg.setVisibility((titleContentStr.size()==0&&commentContentStr.size()==0) ? View.GONE : View.VISIBLE);
                    mBinding.tvTitle.setText(titleContentStr.size() > 0 ? titleContentStr.get(0) : "");
                }
            }

            @Override
            public void onPageSelected(int i) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        mWikiIndicatorAdapter.setOnItemClickLitener(new CommonIndicatorAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(int position, String url) {
                mBinding.vpImager.setCurrentItem(position, true);
            }
        });
    }

    private Bundle setArguments(int i, ImgVideoDetailFragment mFragment) {
        String mUrl = mUrls.get(i);
        Bundle mBundle = new Bundle();
        int type;
        if (!TextUtils.isEmpty(mUrl) && mUrl.contains("videotx") && (!mUrl.endsWith(".jpg")
                && !mUrl.endsWith(".png")
                && !mUrl.endsWith(".jpeg")
                && !mUrl.endsWith(".bmp"))) {
            //视频
            type = ImgVideoDetailFragment.VIDEO_TYPE;
        } else {
            if (mUrl.contains("mp4")) {
                type = ImgVideoDetailFragment.VIDEO_TYPE;
            } else {
                type = ImgVideoDetailFragment.IMG_TYPE;
            }

        }
        mBundle.putString("url", mUrls.get(i));
        mBundle.putInt(Constants.WIKI_IMGPAGER_TYPE, type);
        mFragment.setArguments(mBundle);
        return mBundle;
    }


}
